local xiaoxin = fk.CreateSkill{
  name = "tsp__xiaoxin",
}
Fk:loadTranslationTable{
  ["tsp__xiaoxin"] = "鸮心",
  [":tsp__xiaoxin"] = "回合开始时，或当你需要使用【闪】时，或当你于回合外需要使用【桃】时，你可以拒绝执行之并摸三张牌，"..
    "然后若你的手牌数变为全场唯一最多，本局游戏你于该时机仅能如此做。",
  
  ["#tsp__xiaoxin_jink"] = "鸮心：你可以拒绝使用【闪】并摸三张牌",
  ["#tsp__xiaoxin_peach"] = "鸮心：你可以拒绝使用【桃】并摸三张牌",
  ["#tsp__xiaoxin_turn"] = "鸮心：你可以跳过此回合并摸三张牌",
  
  ["@tsp__xiaoxin_only"] = "鸮心",
  
  ["tsp__xiaoxin_prohibit_turn"] = "回合",
  ["tsp__xiaoxin_prohibit_jink"] = "闪",
  ["tsp__xiaoxin_prohibit_peach"] = "桃",
}

local findOnlyMax = function (room)
  local players = room:getAlivePlayers()
  table.sort(players, function (a, b)
    return a:getHandcardNum() > b:getHandcardNum()
  end)
  if players[1]:getHandcardNum() == players[2]:getHandcardNum() then
    return nil
  else
    return players[1]
  end
end

xiaoxin:addEffect(fk.TurnStart, {
  can_trigger = function (self, event, target, player, data)
    return player == target and player:hasSkill(self)
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local prohibit = player:getTableMark("@tsp__xiaoxin_only")
    if table.contains(prohibit, "tsp__xiaoxin_prohibit_turn") then
      return true
    else
      return room:askToSkillInvoke(player, {skill_name = xiaoxin.name, prompt = "#tsp__xiaoxin_turn"})
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local max_pre = findOnlyMax(room)
    room:drawCards(player, 3, xiaoxin.name) 
    local max_after = findOnlyMax(room)
    if max_pre ~= player and max_after == player then
      room:addTableMarkIfNeed(player, "@tsp__xiaoxin_only", "tsp__xiaoxin_prohibit_turn")
    end
    room:endTurn()
  end
})

xiaoxin:addEffect(fk.AskForCardUse, {
  can_trigger = function (self, event, target, player, data)
    return player == target and player:hasSkill(self) and Exppattern:Parse(data.pattern):matchExp("jink")
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local prohibit = player:getTableMark("@tsp__xiaoxin_only")
    if table.contains(prohibit, "tsp__xiaoxin_prohibit_jink") then
      return true
    else
      return room:askToSkillInvoke(player, {skill_name = xiaoxin.name, prompt = "#tsp__xiaoxin_jink"})
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local max_pre = findOnlyMax(room)
    room:drawCards(player, 3, xiaoxin.name) 
    local max_after = findOnlyMax(room)
    if max_pre ~= player and max_after == player then
      room:addTableMarkIfNeed(player, "@tsp__xiaoxin_only", "tsp__xiaoxin_prohibit_jink")
    end
    room:addPlayerMark(player, "tsp__xiaoxin_prohibit_jink")
    room.logic:getCurrentEvent().parent:addCleaner(function ()
      room:removePlayerMark(player, "tsp__xiaoxin_prohibit_jink")
    end)
  end
})

xiaoxin:addEffect(fk.AskForPeaches, {
  can_trigger = function (self, event, target, player, data)
    return player == target and player:hasSkill(self) and player.phase == Player.NotActive
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local prohibit = player:getTableMark("@tsp__xiaoxin_only")
    if table.contains(prohibit, "tsp__xiaoxin_prohibit_peach") then
      return true
    else
      return room:askToSkillInvoke(player, {skill_name = xiaoxin.name, prompt = "#tsp__xiaoxin_peach"})
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local max_pre = findOnlyMax(room)
    room:drawCards(player, 3, xiaoxin.name) 
    local max_after = findOnlyMax(room)
    if max_pre ~= player and max_after == player then
      room:addTableMarkIfNeed(player, "@tsp__xiaoxin_only", "tsp__xiaoxin_prohibit_peach")
    end
    room:addPlayerMark(player, "tsp__xiaoxin_prohibit_peach")
    room.logic:getCurrentEvent().parent:addCleaner(function ()
      room:removePlayerMark(player, "tsp__xiaoxin_prohibit_peach")
    end)
  end
})

xiaoxin:addEffect("prohibit", {
  prohibit_use = function (self, player, card)
    local disable = false
    if player:getMark("tsp__xiaoxin_prohibit_jink") > 0 then
      disable = disable or card.trueName == "jink"
    end
    if player:getMark("tsp__xiaoxin_prohibit_peach") > 0 then
      disable = disable or card.name == "peach"
    end
    return disable
  end,
})

return xiaoxin
